package com.telnetnz.ticketdesk.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.rockbb.common.utilities.time.TimeUtil;
import com.telnetnz.ticketdesk.bean.ConfigBean;
import com.telnetnz.ticketdesk.common.config.SpringContext;
import com.telnetnz.ticketdesk.dao.entity.ConfigEntity;
import com.telnetnz.ticketdesk.dao.wrapper.ConfigWrap;

public class ConfigService
{
	private static Logger logger = Logger.getLogger(ConfigService.class);

	public static int getIntConfig(String name)
	{
		ConfigEntity entity = getWrap().select(name);
		if (entity != null)
		{
			try {return Integer.parseInt(entity.getValue());}
			catch (Exception e) 
			{
				logger.info("Invalid int value when getting config: "+name);
				return 0;
			}
		}
		else
		{
			logger.info("No result when getting int config for name: "+name);
			return 0;
		}
	}

	public static void updateIntConfig(String name, int newValue)
	{
		ConfigEntity entity = getWrap().select(name);
		if (entity != null)
		{
			String value = newValue+"";
			entity.setValue(value);
			getWrap().update(entity);
		}
		else
		{
			logger.info("No result when getting Interger config for name: "+name);
		}
	}

	public static String getConfig(String name)
	{
		ConfigEntity entity = getWrap().select(name);
		if (entity != null)
		{
			return entity.getValue();
		}
		else
		{
			logger.info("No result when getting String config for name: "+name);
			return null;
		}
	}

	public static void updateConfig(String name, String newValue)
	{
		ConfigEntity entity = getWrap().select(name);
		if (entity != null)
		{
			entity.setValue(newValue);
			getWrap().update(entity);
		}
		else
		{
			logger.info("No result when updating String config for name: "+name);
		}
	}

	public static Date getDateConfig(String name)
	{
		ConfigEntity entity = getWrap().select(name);
		if (entity != null)
		{
			try {return TimeUtil.getDate(entity.getValue(), "yyyy-MM-dd HH:mm:ss");}
			catch (Exception e) 
			{
				logger.info("Invalid date value when getting config: "+entity.getValue());
				return null;
			}
		}
		else
		{
			logger.info("No result when getting config for name: "+name);
			return null;
		}
	}

	public static void updateDateConfig(String name, Date newDate)
	{
		ConfigEntity entity = getWrap().select(name);
		if (entity != null)
		{
			String value = TimeUtil.getStr(newDate, "yyyy-MM-dd HH:mm:ss");
			entity.setValue(value);
			getWrap().update(entity);
		}
		else
		{
			logger.info("No record when updating config for name: "+name);
		}
	}

	public static List<ConfigBean> getAll()
	{
		List<ConfigEntity> entities = getWrap().selectAll();
		return adaptList(entities);
	}

	public static void update(ConfigBean bean)
	{
		if (bean == null)
			return;
		getWrap().update(bean.toEntity());
	}

	private static List<ConfigBean> adaptList(List<ConfigEntity> entities)
	{
		List<ConfigBean> beans = new ArrayList<ConfigBean>();
		if (entities != null && entities.size() > 0)
		{
			for (int i = 0; i < entities.size(); i++)
			{
				ConfigBean bean = ConfigBean.adapt(entities.get(i));
				if (bean == null)
					continue;
				beans.add(bean);
			}
		}
		return beans;
	}

	private static ConfigWrap getWrap()
	{
		return SpringContext.getConfigWrap();
	}

	public static void main(String[] args)
	{
		
	}
}
